package com.cloud.drore.eboss.supplier.service.impl;

import com.cloud.drore.eboos.common.base.PagePo;
import com.cloud.drore.eboos.common.base.ResultMessage;
import com.cloud.drore.eboos.common.constant.CommonConstant;
import com.cloud.drore.eboos.common.util.LocalData;
import com.cloud.drore.eboss.operation.entity.po.DistributorInfoPo;
import com.cloud.drore.eboss.operation.entity.po.DistributorsAllRelationPo;
import com.cloud.drore.eboss.operation.entity.vo.DistributorInfoVo;
import com.cloud.drore.eboss.operation.entity.vo.DistributorsAllRelationVo;
import com.cloud.drore.eboss.supplier.entity.po.SupplierAndDistributorRelationPo;
import com.cloud.drore.eboss.supplier.mapper.SuppliersRelationMapper;
import com.cloud.drore.eboss.supplier.operationApiService.ISupplierAndDistributorRelationService;
import com.cloud.drore.eboss.util.DictionaryMapUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 浙江卓锐科技股份有限公司 版权所有  Copyright 2018<br>
 *
 * @Description: <br>
 * @Project: eboos <br>
 * @CreateDate: Created in 2018/3/23 10:33 <br>
 * @Author: <a href="1451173193@qq.com">杨亮</a>
 */
@Service
public class SupplierAndDistributorRelationServiceImpl implements ISupplierAndDistributorRelationService {
   @Resource
    private SuppliersRelationMapper suppliersRelationMapper;
    @Override
    public ResultMessage<List<SupplierAndDistributorRelationPo>> getAllDistributorBySupplierId(Long supplierId){
        List<SupplierAndDistributorRelationPo> list=suppliersRelationMapper.getAllDistributorBySupplierId(supplierId);
        return ResultMessage.success(list);
    }

    /**
     * @ClassName: SupplierAndDistributorRelationServiceImpl
     * @param pagePo
     * @Return: com.cloud.drore.eboos.common.base.ResultMessage<java.util.List < com.cloud.drore.eboss.supplier.entity.po.SupplierAndDistributorRelationPo>>
     * @Decription:供应商合作管理模块
     * @CreateDate: Created in 2018/4/9 17:31
     * @Author: <a href="1451173193@qq.com">杨亮</a>
     * @Modify:
     */
    @Override
    public ResultMessage<List<DistributorInfoVo>> getDistributorInfoListBySupplier(PagePo<DistributorInfoPo> pagePo) {

        DistributorInfoPo vo= pagePo.getFields();
        vo.setSupplierId(LocalData.getCurrentUser().getTypeId());
        PageHelper.startPage(pagePo);
        Map<String, String> industryMap = DictionaryMapUtil.getMapByParentCode(CommonConstant.MERCHANT_INDUSTRY);
        List<DistributorInfoVo> list=suppliersRelationMapper.getDistributorInfoListBySupplier(vo);
        list.stream().map(item->{item.setDistributorType(industryMap.get(item.getDistributorType())); return item;}).collect(Collectors.toList());
        PageInfo pageInfo = new PageInfo<>(list);
        return  ResultMessage.success(pageInfo);
    }

    @Override
    public ResultMessage getDistributorsAllRelation(PagePo<DistributorsAllRelationPo> pagePo) {
        PageHelper.startPage(pagePo);
        DistributorsAllRelationPo po=pagePo.getFields();
        po.setSupplierId(LocalData.getCurrentUser().getTypeId());
        List<DistributorsAllRelationVo> list= suppliersRelationMapper.getDistributorsAllRelation(po);
        if(list!=null&&list.size()>0) {
            Map<String, String> productMap = DictionaryMapUtil.getMapByParentCode(CommonConstant.PRODUCT_TYPE);
            list.stream().map(item -> {
                item.setProductType(productMap.get(item.getProductType()));
                return item;
            }).collect(Collectors.toList());
        }
        PageInfo pageInfo = new PageInfo<>(list);
        return ResultMessage.success(pageInfo);
    }
}
